package study.intconsulting.transform;

import java.util.List;

//import study.intconsulting.net.io.HongClass;
import study.intconsulting.net.io.HongClass;
import study.intconsulting.util.JaxbUtil;
import study.intconsulting.util.XstreamUtil;
import study.intconsulting.vo.PcInfo;
import study.intconsulting.vo.TestPcInfo;
import study.intconsulting.vo.TestPcinfoList;

public class Trans {

	
	public PcInfo mh(PcInfo pcInfo) throws Exception{
		String resultMsg = null;
		PcInfo resultPcInfo = null;
		
		HongClass hongClass = new HongClass();
//		resultMsg = hongClass.call(makeXML(pcInfo));
		
		// 1. jaxb use
		resultMsg = hongClass.call(JaxbUtil.convertToXml(pcInfo, new PcInfo().getClass()));
		
		// 2. xstream use
//		resultMsg = hongClass.call(XstreamUtil.convertToXml(pcInfo, new PcInfo().getClass(), "message"));

		System.out.println("resultMsg : " + resultMsg);
		if(resultMsg != null) {
//			resultPcInfo = makePcInfo(resultMsg);
			
			// 1. jaxb use
			resultPcInfo = (PcInfo)JaxbUtil.convertToPojo(resultMsg, new PcInfo().getClass());
			
			// 2. xstream use
//			resultPcInfo = (PcInfo)XstreamUtil.convertToPojo(resultMsg, new PcInfo().getClass(), "message");
			
    		System.out.println("getIp : " + resultPcInfo.getIp());
    		System.out.println("getName : " + resultPcInfo.getName());
    		System.out.println("getWho : " + resultPcInfo.getWho());

		} else {
			resultPcInfo = new PcInfo();
		}
		return resultPcInfo;
	}
	
	/*
	public String makeXML(PcInfo pcInfo){
		StringBuffer xml = new StringBuffer();
		
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\n");
		if(pcInfo == null){
			xml.append("<pcInfo/>").append("\n");
		} else {
			xml.append("<pcInfo>").append("\n");
			xml.append(assembleTag("ip", pcInfo.getIp())).append("\n");
			xml.append(assembleTag("name", pcInfo.getName())).append("\n");
			xml.append(assembleTag("who", pcInfo.getWho())).append("\n");
			xml.append("</pcInfo>").append("\n");
		}
		
		return xml.toString();
	}
	
	public PcInfo makePcInfo(String xmlString) {
		PcInfo pcInfo = new PcInfo();
		
		if(xmlString == null || xmlString.length() == 0){
			return pcInfo;
		}
		
		ByteArrayInputStream input = new ByteArrayInputStream(xmlString.getBytes());
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		
		try {
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(input);
			
			pcInfo.setIp(nodeValue(doc.getElementsByTagName("ip")));
			pcInfo.setName(nodeValue(doc.getElementsByTagName("name")));
			pcInfo.setWho(nodeValue(doc.getElementsByTagName("who")));
			
		} catch (ParserConfigurationException e) {
			// docBuilderFactory.newDocumentBuilder
			e.printStackTrace();
		} catch (SAXException e) {
			// docBuilder.parse
			e.printStackTrace();
		} catch (IOException e) {
			// docBuilder.parse
			e.printStackTrace();
		} finally{
			try {
				if(input != null) input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return pcInfo;
		
		
	}
	
	// 태그 만들기
	public String assembleTag(String tagName, String tagValue){
		StringBuffer tag = new StringBuffer();
		if(tagValue != null && tagValue.length() > 0){
			tag.append("<").append(tagName).append(">").append(tagValue).append("</").append(tagName).append(">");
		} else {
			tag.append("<").append(tagName).append("/>");
		}
		return tag.toString();
	}
	
	// 태그 값 가져오기
	public String nodeValue(NodeList nodeList){
		String value = "";
		if(nodeList != null && nodeList.getLength() > 0 && nodeList.item(0).hasChildNodes()){
			value = nodeList.item(0).getFirstChild().getNodeValue();
		}
		return value;
	}
	*/
	
	public static void main(String[] arg){
        System.out.println("================================================================================");
        System.out.println("                                  Jaxb                                          ");
        System.out.println("================================================================================");
		/*
		 *  1. Jaxb Test..
		 */
        
		// 1.1 simple
        System.out.println("============================simple==============================");
        String JaxbXml1 = JaxbUtil.convertToXml(new PcInfo("", "", "who"), new PcInfo().getClass());
        System.out.println("JaxbXml1 : " + JaxbXml1);

        PcInfo resPcInfo = (PcInfo)JaxbUtil.convertToPojo(JaxbXml1, new PcInfo().getClass());
        System.out.println("getIp : " + resPcInfo.getIp());
        System.out.println("getName : " + resPcInfo.getName());
        System.out.println("getWho : " + resPcInfo.getWho());
        
        
        // 1.2 list
        System.out.println();
        System.out.println("============================list==============================");
        
        String firstAttribute = "test";
        String secondAttribute = "테스트";

        TestPcinfoList testPcinfoList = new TestPcinfoList();
        testPcinfoList.setManagerId(1);
        testPcinfoList.getPcinfoList().add(new TestPcInfo(firstAttribute, "ip1", "name1", "who1"));
        testPcinfoList.getPcinfoList().add(new TestPcInfo(secondAttribute, "ip2", "name2", "who2"));

        String JaxbXml2 = JaxbUtil.convertToXml(testPcinfoList, new TestPcinfoList().getClass());

        String xpathExpression = "/message/data/@attribute";
        String actual = JaxbUtil.extractValue(JaxbXml2, xpathExpression);
        System.out.println("actual : " + actual);
        
        try{
        	TestPcinfoList resPcinfoList = (TestPcinfoList)JaxbUtil.convertToPojo(JaxbXml2, new TestPcinfoList().getClass());

        	List<TestPcInfo> infoList = resPcinfoList.getPcinfoList();
        	for(int i=0;i<infoList.size();i++){
        		TestPcInfo temp = infoList.get(i);
        		System.out.println("getIp : " + temp.getIp());
        		System.out.println("getName : " + temp.getName());
        		System.out.println("getWho : " + temp.getWho());
        	}
        }catch(Exception e){
        	e.printStackTrace();
        }
        
        System.out.println();
        System.out.println("================================================================================");
        System.out.println("                                 Xstream                                        ");
        System.out.println("================================================================================");
        /*
         * 2. Xstream test..
         */
        // 2.1 simple
        System.out.println("============================simple==============================");
		String XstreamXml1 = XstreamUtil.convertToXml(new PcInfo("", "", "who_0"), new PcInfo().getClass(), "message");
		System.out.println(XstreamXml1);
		PcInfo info = (PcInfo)XstreamUtil.convertToPojo(XstreamXml1, new PcInfo().getClass(), "message");
		System.out.println(info);
        
        // 2.2 list
        System.out.println();
        System.out.println("============================list==============================");
        String XstreamXml2 = XstreamUtil.convertToXml(testPcinfoList, new TestPcinfoList().getClass(), "message");
        System.out.println("XstreamXml2 : " + XstreamXml2);
        
        try{
        	TestPcinfoList resPcinfoList = (TestPcinfoList)XstreamUtil.convertToPojo(XstreamXml2, new TestPcinfoList().getClass(), "message");
        	List<TestPcInfo> infoList = resPcinfoList.getPcinfoList();
        	for(int i=0;i<infoList.size();i++){
        		TestPcInfo temp = infoList.get(i);
        		System.out.println("getIp : " + temp.getIp());
        		System.out.println("getName : " + temp.getName());
        		System.out.println("getWho : " + temp.getWho());
        	}
        }catch(Exception e){
        	e.printStackTrace();
        }
        
        /*
         * 3. Xmlbeans test..
         */
        System.out.println();
        System.out.println("================================================================================");
        System.out.println("                                 Xmlbeans                                        ");
        System.out.println("================================================================================");
        // 3.1 simple
        
        // 3.2 list
        
	}	
}
